package work.vcloud.baseinfo.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import work.vcloud.baseinfo.utils.HttpUtil;
import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Scope("prototype")
public class ClassController extends BaseController {

	@Autowired
	OpenDao openDao;

	/**
	 * 新增班级信息
	 */
	@RequestMapping("api/class.insert")
	public OutputVo insert() {
		try {

			Map<String, Object> map = getParameterMap();
			map.remove("page");

			//插入班级
			map.put("sqlMapId", "class.insert");
			map.put("fk_class", openDao.insert(map));

			//获取当前学年
			Map prop =  new HashMap();
			prop.put("prop_key","STU_YEAR");
			prop.put("status","0");
			prop.put("sqlMapId", "prop.select");
			Map propMap = (Map) openDao.queryForObject(prop);

			//当前学年
			int real_value = Integer.parseInt(propMap.get("real_value").toString());

			//获取当前学期
			prop.put("prop_key","STU_TERM");
			prop.put("status","0");
			prop.put("sqlMapId", "prop.select");
			Map teamMap = (Map) openDao.queryForObject(prop);

			//判断是否有专业
			if(map.containsKey("fk_major")){
				if(!map.get("fk_major").equals("")){
					//获取专业年限
					Map major =  new HashMap();
					major.put("id",map.get("fk_major"));
					major.put("sqlMapId", "major.selectById");
					Map majorMap = (Map) openDao.queryForObject(major);

					//专业年限
					int years = Integer.parseInt(majorMap.get("years").toString());

					//获取学期
					prop.put("prop_key","STU_TERM");
					prop.put("status","");
					prop.put("sqlMapId", "prop.select");
					List<Map<String, Object>> propList = openDao.queryForList(prop);

					//循环年限
					int num = 0;
					for(int i = 0 ; i < years ; i++){
						Map ClassProp = new HashMap();
						for(Map team:propList){

							//获取学年pk
							Map year = new HashMap();
							prop.put("prop_key","STU_YEAR");
							prop.put("real_value",real_value+i);
							prop.put("sqlMapId", "prop.select");
							Map yearMap = (Map) openDao.queryForObject(prop);

							if(yearMap == null){
								//新增学年
								Map yearInsert = new HashMap();
								yearInsert.put("prop_key","STU_YEAR");
								yearInsert.put("real_value",(real_value+i)+"");
								yearInsert.put("prop_value",(real_value+i)+"学年");
								yearInsert.put("prop_remark","学年");
								yearInsert.put("status","1");
								yearInsert.put("userInfo",map.get("userInfo"));
								yearInsert.put("sqlMapId", "prop.insert");
								ClassProp.put("fk_year",openDao.insert(yearInsert));
							}else{
								ClassProp.put("fk_year",yearMap.get("prop_pk"));
							}

							ClassProp.put("fk_class",map.get("fk_class"));
							ClassProp.put("fk_team",team.get("prop_pk"));

							//序号
							num +=1;
							ClassProp.put("sort",num);

							//新增班级学年学期绑定
							ClassProp.put("sqlMapId", "class.insertClassProp");
							ClassProp.put("userInfo",map.get("userInfo"));
							openDao.insert(ClassProp);
						}


					}

				}else{
					Map ClassProp = new HashMap();
					ClassProp.put("fk_class",map.get("fk_class"));
					ClassProp.put("fk_year",propMap.get("prop_pk"));
					ClassProp.put("fk_team",teamMap.get("prop_pk"));
					ClassProp.put("sort",1);

					//新增班级学年学期绑定
					ClassProp.put("sqlMapId", "class.insertClassProp");
					ClassProp.put("userInfo",map.get("userInfo"));
					openDao.insert(ClassProp);
				}
			}else{
				Map ClassProp = new HashMap();
				ClassProp.put("fk_class",map.get("fk_class"));
				ClassProp.put("fk_year",propMap.get("prop_pk"));
				ClassProp.put("fk_team",teamMap.get("prop_pk"));
				ClassProp.put("sort",1);

				//新增班级学年学期绑定
				ClassProp.put("sqlMapId", "class.insertClassProp");
				ClassProp.put("userInfo",map.get("userInfo"));
				openDao.insert(ClassProp);
			}

			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}

	/**
	 * 修改班级学年学期
	 * @return
	 */
	@RequestMapping("api/updateClassYearTerm")
	public OutputVo updateClassYearTerm() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");

			//修改班级学年学期
			map.put("sqlMapId", "class.updateClassYearTerm");
			int affectedRows = openDao.update(map);
			if (affectedRows > 0) {
				//查询所有班级
				map.put("sqlMapId", "class.findClassId");
				List<Map<String, Object>> dataList= openDao.queryForList(map);
				for (Map<String, Object> item : dataList) {
					map.put("fk_class", item.get("id"));
					map.put("sort", Integer.parseInt(item.get("sort").toString()));
					map.put("fk_team", item.get("semester"));
					map.put("fk_year", item.get("academic"));
					map.put("sqlMapId", "class.findClassProp");
					List<Map<String, Object>> subDataList = openDao.queryForList(map);
					if (subDataList.size() < 1) { //学年学期班级是否存在，如不存在则插入
						map.put("sqlMapId", "class.insertClassProp");
						openDao.insert(map);
					}
				}
			}
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}


}
